package com.sti.platform.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aisino.core.common.JsonPost;
import com.aisino.core.controller.BaseController;
import com.aisino.tools.util.WebConstants;
import com.sti.platform.service.ActivitiService;
import com.sti.platform.service.BusinossService;


@Controller()
@RequestMapping("/ti/activiti")
public class ActivitiController extends BaseController{
	
	@Autowired
	private ActivitiService activitiService;
	@Autowired
	private BusinossService businossService;
	
	@RequestMapping("lx")
	public String lxPage(HttpServletRequest request){
		String userId = "300003";
		List<Map<String,Object>> list = businossService.queryStartUpBusiness(userId);
		request.setAttribute("list", list);
		return "technology/lixiang";
	}
	
	@RequestMapping("list")
	public String auditPage(){
		return "technology/audit";
	}

	
	/**
	 * 审核任务
	 * @return
	 */
	@RequestMapping("audit")
	public @ResponseBody Map  auditPage(final String taskId,final String users,final String opinion,final String userId){
		return super.jsonPost(new JsonPost(){
			@SuppressWarnings("unchecked")
			public void doInstancePost(Map map){
				map.put(WebConstants.JSON_MESSAGE, activitiService.completeTask(taskId,users,opinion,userId));
			}
		});
	}
	
	/**
	 * 发起流程
	 * @return
	 */
	@RequestMapping("submit")
	public String submitPage(HttpServletRequest request){
		String title = request.getParameter("title");
		String context = request.getParameter("context");
		/**
		 * 启动流程，并返回业务信息
		 */
		List<Map<String,Object>> list = activitiService.createProcessInstance(title,context);
		
		request.setAttribute("list", list);
		return "technology/lixiang";
		
	}
	
	/**
	 * 查询当前用户的任务
	 * @param userId 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("queryTask")
	public String queryTsakPage(HttpServletRequest request) throws IOException{
		String userId = request.getParameter("userId");
		List<Map<String,Object>> list = activitiService.queryTask(userId);
		
		request.setAttribute("list", list);
		return "technology/audit";
	}
	
	/**
	 * 回退到指定环节
	 * @param taskId 任务ID
	 * @param usertaskId 跳转到环节ID  
	 * @return
	 */
	@RequestMapping("taskRoll")
	public @ResponseBody Map updateTaskRollPage(final String taskId,final String usertaskId){
		return super.jsonPost(new JsonPost(){
			@SuppressWarnings("unchecked")
			public void doInstancePost(Map map){
				map.put(WebConstants.JSON_MESSAGE, activitiService.updateTaskRoll(taskId,usertaskId));
			}
		});
	}
	
	/**
	 * 获取专家
	 * @param taskId
	 * @return
	 * @throws IOException 
	 * @throws Exception 
	 */
	@RequestMapping("saveUser")
	public @ResponseBody Map queyrUserPage() throws Exception{
		return super.jsonPost(new JsonPost(){
			@SuppressWarnings("unchecked")
			public void doInstancePost(Map map){
				map.put(WebConstants.JSON_MESSAGE, activitiService.queryUser());
			}
		});
	}
}
